/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;

/**
 *
 * @author TQNINH
 */
public class BinFile {
    
    public static void main(String[] args){       
        int n = 123; //4 bytes
        String st = "Bye"; //6 bytes ? 5 bytes
        //String st = "a"; //3 bytes
        //String st = ""; //2 bytes
        float x = 20.8F; //4 bytes
        boolean f = true; //1 bytes
        
        //System.out.println(st.length());
        File file = new File("D:\\01_Aptech\\03-Sem2-JP2\\project\\Data.bin");
        
        try {
            FileOutputStream fos = new FileOutputStream(file, true);
            try (DataOutputStream dos = new DataOutputStream(fos)) {
                dos.writeInt(n);
                dos.writeUTF(st);
                dos.writeFloat(x);
                dos.writeBoolean(f);
            }
        } catch (IOException e) {
            
        }
        
        try {
            FileInputStream fis = new FileInputStream(file);
            DataInputStream dis = new DataInputStream(fis);
            
            System.out.println("The content of file: ");
            while((n = dis.readInt()) != -1) {
                System.out.print(n + "\t");
                System.out.print(dis.readUTF() + "\t");
                System.out.print(dis.readFloat() + "\t");
                System.out.println(dis.readBoolean() + "\t");
            }
        } catch(IOException e) {
            
        }
    }
}
